package presentation;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import control.CControl;

public class PNetworkStatus extends JPanel {
	private static final long serialVersionUID = 1L;
	private CControl cControl;
	private String[] nodes;
	private PNodePanel[] nodePanels;

	public PNetworkStatus(CControl cControl) {
		this.cControl = cControl;
		
		nodes = this.cControl.getNodeList();
		this.setBorder(new EmptyBorder(10, 10, 10, 10));
		this.setLayout(new GridLayout((int) Math.ceil(nodes.length/2.),2,10,10));
		this.setBackground(Color.white);
		
		nodePanels = new PNodePanel[nodes.length];
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].equalsIgnoreCase("master")) {
				nodePanels[i] = new PNodePanel(nodes[i],0,cControl);
				this.cControl.addRegisterObserver(cControl.getRegisterName(nodes[i], "StatusReg"), nodePanels[i]);
			} else {
				nodePanels[i] = new PNodePanel(nodes[i],i,cControl);
				this.cControl.addRegisterObserver("MN_NodesActive", nodePanels[i]);
			}
			
			this.add(nodePanels[i]);
		}
		
		if (nodePanels.length%2 == 1) {
			JPanel spaceFiller = new JPanel();
			spaceFiller.setBackground(Color.white);
			this.add(spaceFiller);
		}
	}
}
